
#import "CalculatedInfoWebViewController.h"
#import "CustomNavigationBar.h"
#import "UINavigationItem+Customization.h"

@interface CalculatedInfoWebViewController ()

@end

@implementation CalculatedInfoWebViewController

- (id)initWithDefaultNibAndDocumentName:(NSString *)documentName
{
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        self.documentName = documentName;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UINavigationItem *item = self.navigationBar.items[0];
    UIButton *done = [self.navigationBar backButtonWith:[UIImage imageNamed:@"btn-resultat-klar-off"] highlight:[UIImage imageNamed:@"btn-resultat-klar-on"] leftCapWidth:14.0 title:_(@"Klar")];
    done.titleLabel.textColor = [UIColor whiteColor];
    done.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:12];
    [done addTarget:self action:@selector(doneButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    item.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:done];

    UIButton *infoButton = [[UIButton alloc] initWithFrame:CGRectZero];
    [infoButton setImage:[UIImage imageNamed:@"btn-info-off.png"] forState:UIControlStateNormal];
    [infoButton setImage:[UIImage imageNamed:@"btn-info-on.png"] forState:UIControlStateHighlighted];
    [infoButton setImage:[UIImage imageNamed:@"btn-info-on.png"] forState:UIControlStateSelected];
    [infoButton sizeToFit];
    [infoButton addTarget:self action:@selector(showInfo) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *infoBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
    item.rightBarButtonItem = infoBarButtonItem;

    
    item.title = self.title;
    [item customizeTitleInNavigationBarWithHeight:self.navigationController.navigationBar.frame.size.height];
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.png"]];
    
    NSString *locale = @"sv";
    NSString *file = [locale stringByAppendingFormat:@"_%@", self.documentName];
    NSLog(@"%@", file);
    NSString *html = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] 
                                                         pathForResource:file
                                                         ofType:@"html"] 
                                               encoding:NSUTF8StringEncoding error:nil];
    html = [html stringByReplacingOccurrencesOfString:@"../" withString:@""];
    [self.webView loadHTMLString:html baseURL:[[NSBundle mainBundle] bundleURL]]; 
    
    self.topViewSubTitle.text = self.subTitle;
}

- (void)viewDidUnload
{
    [self setTopViewSubTitle:nil];
    [self setTopViewTitle:nil];
    [self setWebView:nil];
    [self setDocumentName:nil];
    [self setBackButtonTitle:nil];
    [self setNavigationBar:nil];
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - selectors
- (void)doneButtonPressed
{
    [self dismissModalViewControllerAnimated:YES];
}

- (void)showInfo
{

}

@end
